package com.aaa.service.impl;

import com.aaa.controller.RoleController;
import com.aaa.dao.MenuMapper;
import com.aaa.dao.RoleMapper;
import com.aaa.entity.Emp;
import com.aaa.entity.Menu;
import com.aaa.entity.Permission;
import com.aaa.entity.Role;
import com.aaa.service.EmpService;
import com.aaa.service.MenuService;
import com.aaa.service.PermissionService;
import com.aaa.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Resource
    MenuMapper menuMapper;
    @Resource
    PermissionService permissionService;
    @Resource
    RoleService roleService;
    @Resource
    EmpService empService;
    @Override
    public List<Map<String, Object>> findMenu(String eusername) {//查询菜单表下的一级和二级菜单并返回
        //查询菜单
        List<Map<String, Object>> list = new ArrayList<>();
        list = menuMapper.findMenuOne(0);//一级菜单
        //一级菜单 查询结果是list<map> 应为实体类没有二级菜单
        int[] mid = new int[list.size()];//mid 用来接收一级菜单的mid
        for (int i=0;i<list.size();i++){
            mid[i]= (int) list.get(i).get("mid");
        }
        //根据登录得用户名查出对应的角色id
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("eusername",eusername);
        List<Emp> empList=empService.list(queryWrapper);
        int roleId=empList.get(0).getRoleid();
        for (int i=0;i<mid.length;i++){//向每一个一级菜单里put二级菜单
            List<Map<String, Object>> menuTwo = menuMapper.findMenuTwo(mid[i],roleId);//二级菜单
            list.get(i).put("menuTwo",menuTwo);
        }
        return list;
    }
}
